<?php
/**
 * 销售物料数据验证Lib类
 * 
 * @access public
 * @author wangbiao<wangbiao@shopex.cn>
 * @version $Id: check.php 2016-08-03 15:00
 */
class material_sales_check
{
    /**
     * 数据检验有效性
     * 
     * @param  Array   $params
     * @param  String  $err_msg
     * @return Boolean
     */
    public function checkParams(&$params, &$err_msg)
    {
        $salesMaterialObj = app::get('material')->model('sales_material');
        
        //新增标记
        $is_new_add    = $params['edit'] ? false : true;
        unset($params['edit']);
        
        if(empty($params['sales_material_name'])){
            $err_msg ="销售物料名称不能为空";
            return false;
        }
        
        if(empty($params['sales_material_bn'])){
            $err_msg ="销售物料编码不能为空";
            return false;
        }
        
        //检查有效性
        if($is_new_add)
        {
            //判断物料编码只能是由数字英文下划线组成
            $reg_bn_code = "/^[0-9a-zA-Z\_\-]*$/";
            if(!preg_match($reg_bn_code,$params["sales_material_bn"])){
                $err_msg = "物料编码只能是数字英文下划线组成";
                return false;
            }
            
            $salesMaterialInfo = $salesMaterialObj->getList('sales_material_bn',array('sales_material_bn'=>$params['sales_material_bn']));
            if($salesMaterialInfo){
                $err_msg ="当前新增的物料编码已被使用，不能重复";
                return false;
            }
            
            $params['sales_material_bn_crc32'] = sprintf('%u',crc32($params['sales_material_bn']));
        }
        else 
        {
            if(empty($params['sm_id'])){
                $err_msg ="销售物料sm_id不能为空";
                return false;
            }
            
            $salesMaterialExistInfo = $salesMaterialObj->getList('sm_id',array('sales_material_bn'=>$params['sales_material_bn']));
            if($salesMaterialExistInfo && $salesMaterialExistInfo[0]['sm_id'] != $params['sm_id']){
                $err_msg ="当前编辑的物料编码已被使用，不能重复";
                return false;
            }
            
            $salesMaterialInfo = $salesMaterialObj->dump($params['sm_id']);
            if(!$salesMaterialInfo){
                $err_msg ="当前物料不存在";
                return false;
            }
        }
        
        if($params['sales_material_type'] == 2){
            if(!isset($params['at'])){
                $err_msg ="促销物料请至少设置一个物料明细内容";
                return false;
            }

            foreach ($params['at'] as $val){
                if (count($params['at']) == 1){
                    if ($val <2){
                        $err_msg ="只有一种物料时，数量必须大于1";
                        return false;
                    }
                }else {
                    if ($val < 1){
                        $err_msg ="数量必须大于0";
                        return false;
                    }
                }
            }

            foreach ($params['pr'] as $val){
                $tmp_rate +=$val;
            }

            if($tmp_rate > 100){
                $err_msg ="分摊销售价合计百分比:".$tmp_rate.",已超100%";
                return false;
            }elseif($tmp_rate < 100){
                $err_msg ="分摊销售价合计百分比:".$tmp_rate.",不足100%";
                return false;
            }
        }
        
        return true;
    }

    /**
     * 检查基础物料个别参数是否可编辑
     * 
     * @param Int $bm_id
     * @return Array
     */
    function checkEditReadOnly($shop_id, $sm_id, $is_bind){
        $readonly = array('type' => false,'shop'=>false,'bind_item'=>false);

        //如果销售物料有冻结、订单，那么物料类型不能变
        if($shop_id != '_ALL_'){
            $shopFreezeObj = app::get('material')->model('sales_material_shop_freeze');
            $storeInfo = $shopFreezeObj->getList('shop_freeze',array('sm_id'=>$sm_id,'shop_id'=>$shop_id));
            if($storeInfo[0]['shop_freeze'] > 0){
                $is_type_readonly = true;
            }
        }

        $orderObjObj = app::get('ome')->model('order_objects');
        $orderInfo = $orderObjObj->getList('obj_id,goods_id',array('goods_id'=>$sm_id));
        if($orderInfo){
            $has_object = true;
            $is_type_readonly = true;
            $is_shop_readonly = true;
        }

        if($is_type_readonly){
            $readonly['type'] = true;
        }

        if($is_shop_readonly){
            $readonly['shop'] = true;
        }

        //已绑定有订单的不能变
        if($is_bind == 1 && $has_object == true){
            $orderItemObj = app::get('ome')->model('order_items');
            $orderItemInfo = $orderItemObj->db->select("select item_id from sdb_ome_order_items as oi left join sdb_ome_order_objects as oo on oi.obj_id = oo.obj_id where goods_id = ".$sm_id." LIMIT 0,1");
            if($orderItemInfo){
                $is_bind_item_readonly = true;
            }
        }

        if($is_bind_item_readonly){
            $readonly['bind_item'] = true;
        }

        return $readonly;
    }
}